package com.lanou.musicbox.fragment.onlinemusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.Singer;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

/**
 * Created by 柳柏庆 on 15/10/27.
 * 网络音乐_进入歌手页面
 */
public class EnterSingersFragment extends Fragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private View view;//定义头布局
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;//网络加载图片属性
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private Toolbar toolbar;
    private ImageView singers_pic, enterSingers_back;
    private Singer singer;
    private String pic_url, string_name;//
    private RadioGroup radio_singers;
    private RadioButton singers_enter_songs, singers_infor;
    private FragmentManager fragmentManager;//碎片管理化属性
    private String messagesurl;
    private String songs_id;
    private String url;
    private  FragmentTransaction transaction;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //设置工具栏标题
        view = inflater.inflate(R.layout.singers_enter_list, null);
        collapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.singers_collapsing_toolbar);
        //给页面设置工具栏
        toolbar = (Toolbar) view.findViewById(R.id.singers_toolbar);
        singers_pic = (ImageView) view.findViewById(R.id.singers_pic);
        enterSingers_back = (ImageView) view.findViewById(R.id.EnterSingers_back);
        singers_enter_songs = (RadioButton) view.findViewById(R.id.singers_enter_songs);
        singers_infor = (RadioButton) view.findViewById(R.id.singers_infor);
        radio_singers= (RadioGroup) view.findViewById(R.id.radio_singers);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /**所有初始化id**/


        //写出监听事件
        radio_singers.setOnCheckedChangeListener(this);
        enterSingers_back.setOnClickListener(this);
        //网络请求初始化
        //图片加载初始化
        volleySingleton = VolleySingleton.getInstance(getActivity());
        imageLoader = volleySingleton.getImageLoader();
        //是否需要折叠效果
        collapsingToolbarLayout.setTitleEnabled(true);
        //获取传过来的值
        Bundle bundle = getArguments();
        songs_id = bundle.getString("songs_id");
        Log.i("出来吧", songs_id + "  ");
        pic_url = bundle.getString("singer_img");//获取传过来的图片
        string_name = bundle.getString("name");//名字对象
        url = Contant.Url.URL_Enter_Singers_frist + songs_id + Contant.Url.URL_Enter_Singers_end;
        Log.i("lanou111111111", "onActivityCreated_url "+url);
        messagesurl = "http://api.dongting.com/song/singer/" + songs_id + "?utdid=VRYtEslzAYQDANmUJ4O6onb7&detail=true";
        Log.i("33333", messagesurl + "");


        //初始构造的方法
        initView();
    }

    private void initView() {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(singers_pic, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(pic_url, listener);
        collapsingToolbarLayout.setTitle(string_name);
        int i = R.id.singers_enter_songs;
        Check(i);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /**按键返回*/
            case R.id.EnterSingers_back:
                getFragmentManager().popBackStack();
                break;
        }
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Log.i("lanou565655", "onCheckedChanged "+ checkedId);
       Check(checkedId);
    }
    private void Check(int i){
        switch (i) {
            case R.id.singers_enter_songs:
                Log.i(" lanou565655","oncerererer");
                fragmentManager = getActivity().getSupportFragmentManager();
                transaction = fragmentManager.beginTransaction();
                SingersLeftFragment fragment = new SingersLeftFragment();
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                bundle.putString("img",pic_url);
                fragment.setArguments(bundle);
                transaction.replace(R.id.singers_fragment, fragment);
                transaction.commit();
                break;
            case R.id.singers_infor:
                Log.i("lanou565655", "onCheckedChanged_2 ");
                fragmentManager = getActivity().getSupportFragmentManager();
                transaction = fragmentManager.beginTransaction();
                SingersRightFragment singersRightFragment = new SingersRightFragment();
                Bundle bun = new Bundle();
                bun.putString("messageurl", messagesurl);
                singersRightFragment.setArguments(bun);
                transaction.replace(R.id.singers_fragment, singersRightFragment);
                transaction.commit();
                break;

        }

    }
}
